/**
 * author:Administrator
 * date:2015/7/27
 * desc:TODO
 */

'use strict';

var Router = require('koa-router');
var config = require('../../../config/config.js');
let daoDispatcher = require('../../../dao/shop.js');

console.log('router:' + config.baseUrl + '/v2/shop');
var router = new Router({
    prefix: config.baseUrl + '/v2/shop'
});
module.exports = router.routes();

/**
 * 添加一个shop
 * post /api/v2/shop/
 {
   "name": "shop-test",
   "address": "天河区天河路208号粤海喜来登酒店8楼",
   "tel": "020-66822580",
   "desc": "一家店",
   "imgUrl": ["https://www.baidu.com/img/bdlogo.png"],
   "webUrl":"https://www.baidu.com/",
   "loc": [23.13268,113.322]
 }
 */
router.post('/', function*() {
    let result = yield daoDispatcher.addOne(this.request.inParams);
    this.body = result;
});

/**
 * get by id
 * get /api/v2/shop?id=123
 */
router.get('/', function*() {
    let result = yield daoDispatcher.findById(this.query.id);
    this.body = result;
});

/**
 * get by id
 * get /api/v2/shop/many?id=123,456
 */
router.get('/many', function*() {
    let id = this.query.id.split(',');
    let result = yield daoDispatcher.findByIdList(id);
    this.body = result;
});

/**
 * post /api/v2/shop/cates
 {
     "shopIds": [
         "55c02752329f2fb4042c9582",
         "55c0275f329f2fb4042c9583"
     ],
     "cates": [
         "美容",
         "abc"
     ]
 }
 */
router.post('/cates', function*() {
    let idList = this.request.inParams.shopIds;
    let cates = this.request.inParams.cates;
    let result = yield daoDispatcher.updateCates(idList, cates);
    this.body = result;
});